/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import model.*;
import org.apache.jasper.tagplugins.jstl.ForEach;

/**
 *
 * @author HELLO
 */
@Named(value = "a_albumManagedBean")
@SessionScoped
public class AlbumManagedBean implements Serializable {
    @EJB
    private AlbumFacade albumFacade;
    private List<Album> albums = new ArrayList<Album>();
    private Album album = new Album();
    private Album addAlbum = new Album();

    public Album getAddAlbum() {
        return addAlbum;
    }

    public void setAddAlbum(Album addAlbum) {
        this.addAlbum = addAlbum;
    }

    public AlbumFacade getAlbumFacade() {
        return albumFacade;
    }

    public void setAlbumFacade(AlbumFacade albumFacade) {
        this.albumFacade = albumFacade;
    }

    public List<Album> getAlbums() {
        return albums;
    }

    public void setAlbums(List<Album> albums) {
        this.albums = albums;
    }

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }
    
    public AlbumManagedBean() {
    }
    
    public void getAlbum_All() {
        try {
            setAlbums(this.albumFacade.findAll());
        } catch (Exception e) {
            setAlbums(null);
        }
    }
    
    public String getAvatar(Album a){
        for (Images i : a.getImagesList()) {
            if(i.getAvatar()){
                return i.getImages();
            }   
        }
        return "default_album_avatar.jpg";
    }
    
    public void editAlbum() {
        try {
            this.albumFacade.edit(album);
            
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit Album Sucessed", "You edit ' " + album.getName() + " ' success.");

            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception e) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Edit Album Failled", "You edit ' " + album.getName() + " ' error.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    public void removeAlbum(){
        try {
            this.albumFacade.remove(album);
            getAlbum_All();
            
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Remove Album Sucessed", "You Remove ' " + album.getName() + " ' success.");

            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception e) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Remove Album Failled", "You Remove ' " + album.getName() + " ' error.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    public void createAlbum() {
        try {
            this.albumFacade.create(addAlbum);
            getAlbum_All();           

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Create Album Sucessed", "Album " + addAlbum.getName() + " success."));
            
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Create Album Failled", "Album " + addAlbum.getName() + " error !!!"));
        }
        this.addAlbum = new Album();
    }
    
    public String pageImage(Album a){
        this.album = a;
        return "image";
    }
    
}
